package com.mazaiting.instructions.comparisons;

import com.mazaiting.instructions.base.BranchInstruction;
import com.mazaiting.rtda.MtFrame;

/**
 * if<cond>指令把操作数栈顶的int变量弹出，然后跟0进行比较，满足条件则跳转
 *
 * @author mazaiting
 * @since 2021/8/15
 */
public class IfCondInstruction {
    /**
     * 条件判断相等
     */
    public static class IfEq extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            int val = frame.getOperandStack().popInt();
            if (val == 0) {
                branch(frame, offset);
            }
        }
    }

    /**
     * 条件判断不等于
     */
    public static class IfNe extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            int val = frame.getOperandStack().popInt();
            if (val != 0) {
                branch(frame, offset);
            }
        }
    }

    /**
     * 条件判断小于
     */
    public static class IfLt extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            int val = frame.getOperandStack().popInt();
            if (val < 0) {
                branch(frame, offset);
            }
        }
    }

    /**
     * 条件判断小于等于
     */
    public static class IfLe extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            int val = frame.getOperandStack().popInt();
            if (val <= 0) {
                branch(frame, offset);
            }
        }
    }

    /**
     * 条件判断大于
     */
    public static class IfGt extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            int val = frame.getOperandStack().popInt();
            if (val > 0) {
                branch(frame, offset);
            }
        }
    }

    /**
     * 条件判断大于等于
     */
    public static class IfGe extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            int val = frame.getOperandStack().popInt();
            if (val >= 0) {
                branch(frame, this.offset);
            }
        }
    }
}
